package l1j.server.server.utils;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1EffectInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.Instance.L1SummonInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import static l1j.server.server.model.skill.L1SkillId.*;

/**
 * 對象檢查器
 * 
 * @author daien
 * 
 */
public class CheckUtil {

	private static final Logger _log = Logger.getLogger(CheckUtil.class
			.getName());

	/**
	 * 檢查攻擊致死成立的PC
	 * 
	 * @param lastAttacker
	 * 
	 * @return 攻擊致死成立的PC
	 */
	public static L1PcInstance checkAtkPc(final L1Character lastAttacker) {
		try {
			// 判斷主要攻擊者
			L1PcInstance pc = null;

			if (lastAttacker instanceof L1PcInstance) {// 攻擊者是玩家
				pc = (L1PcInstance) lastAttacker;

			} else if (lastAttacker instanceof L1PetInstance) {// 攻擊者是寵物
				final L1PetInstance atk = (L1PetInstance) lastAttacker;
				if (atk.getMaster() != null) {
					if (atk.getMaster() instanceof L1PcInstance) {
						pc = (L1PcInstance) atk.getMaster();
					}
				}

			} else if (lastAttacker instanceof L1SummonInstance) {// 攻擊者是 召換獸
				final L1SummonInstance atk = (L1SummonInstance) lastAttacker;
				if (atk.getMaster() != null) {
					if (atk.getMaster() instanceof L1PcInstance) {
						pc = (L1PcInstance) atk.getMaster();
					}
				}

				/*
				 * } else if (lastAttacker instanceof L1IllusoryInstance) {//
				 * 攻擊者是 分身 final L1IllusoryInstance atk = (L1IllusoryInstance)
				 * lastAttacker; if (atk.getMaster() != null) { if
				 * (atk.getMaster() instanceof L1PcInstance) { pc =
				 * (L1PcInstance) atk.getMaster(); } }
				 */

			} else if (lastAttacker instanceof L1EffectInstance) {// 攻擊者是 技能物件
				final L1EffectInstance atk = (L1EffectInstance) lastAttacker;
				if (atk.getMaster() != null) {
					if (atk.getMaster() instanceof L1PcInstance) {
						pc = (L1PcInstance) atk.getMaster();
					}
				}

			} else {
				return null;
			}
			return pc;

		} catch (final Exception e) {
			_log.log(Level.WARNING, e.getLocalizedMessage(), e);
		}
		return null;
	}

	/**
	 * 檢查該物件是否允許攻擊(技能)
	 * 
	 * @param cha
	 * @return
	 */
	public static boolean checkAttackSkill(final L1Character cha) {
		try {
			if (cha instanceof L1EffectInstance) { // 效果不列入目標
				return false;

			}
		} catch (final Exception e) {
			_log.log(Level.WARNING, e.getLocalizedMessage(), e);
		}
		return true;
	}

	/**
	 * 該座標點上有物件(不可穿透目標)
	 * 
	 * @param pc
	 *            檢查對象
	 * @param locx
	 * @param locy
	 * @param mapid
	 * @return true有 false沒有
	 */
	public static boolean checkPassable(final L1PcInstance pc, final int locx,
			final int locy, final short mapid) {

		// 1格範圍內物件
		final Collection<L1Object> allObj = L1World.getInstance()
				.getVisibleObjects(pc, 1);
		for (L1Object obj : allObj) {
			// 道具
			if (obj instanceof L1ItemInstance) {
				continue;
			}
			if (!(obj instanceof L1Character)) {
				continue;
			}
			final L1Character character = (L1Character) obj;
			// 忽略隱身
			if (character.isInvisble()) {
				continue;
			}
			if (character instanceof L1NpcInstance) {
				continue;
			}
			if (character instanceof L1PcInstance) {
				final L1PcInstance tgpc = (L1PcInstance) character;
				if (pc.isGmInvis()) {// 忽略GM隱身
					continue;
				}
				if (pc.isGhost()) {// 忽略鬼魂模式
					continue;
				}
				if (tgpc.getX() == locx && tgpc.getY() == locy
						&& tgpc.getMapId() == mapid) {// 其他
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 無法使用指定類型道具
	 * 
	 * @param pc
	 *            檢查對象
	 * @return
	 */
	public static boolean getUseItem(final L1PcInstance pc) {
		/*
		 * 33 木乃伊的詛咒 50 冰矛圍籬 66 沉睡之霧 71 藥水霜化術 87 衝擊之暈 157 大地屏障 189 衝擊之膚 192 奪命之雷
		 */

		if (pc.hasSkillEffect(DECAY_POTION)) {
			pc.sendPackets(new S_ServerMessage(698)); // 喉咙灼热，无法喝东西。
			return false;
		}

		if (pc.hasSkillEffect(EARTH_BIND)) {
			return false;
		}

		if (pc.hasSkillEffect(SHOCK_SKIN)) {
			return false;
		}

		if (pc.hasSkillEffect(THUNDER_GRAB)) {
			return false;
		}

		if (pc.hasSkillEffect(ICE_LANCE)) {
			return false;
		}

		if (pc.hasSkillEffect(FREEZING_BREATH)) {
			return false;
		}
		return true;
	}

	/**
	 * 全物件無法使用
	 * 
	 * @param pc
	 *            檢查對象
	 * @return
	 */
	public static boolean getUseItemAll(final L1PcInstance pc) {
		/*
		 * 33 木乃伊的詛咒 50 冰矛圍籬 66 沉睡之霧 71 藥水霜化術 87 衝擊之暈 157 大地屏障 189 衝擊之膚 192 奪命之雷
		 */
		if (pc.hasSkillEffect(STATUS_CURSE_PARALYZED)) {
			return false;
		}

		if (pc.hasSkillEffect(STATUS_POISON_PARALYZED)) {
			return false;
		}

		if (pc.hasSkillEffect(FOG_OF_SLEEPING)) {
			return false;
		}

		if (pc.hasSkillEffect(SHOCK_STUN)) {
			return false;
		}

		return true;
	}

	private CheckUtil() {
	}
}
